package com.winsdom.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.winsdom.dto.ClaimDTO;
import com.winsdom.dto.PaymentReceiptDTO;
import com.winsdom.dto.RefundOrderDTO;
import com.winsdom.dto.ReceivePaymentDTO;
import com.winsdom.dto.resource.CancelClaimtQueryDTO;
import com.winsdom.entity.PaymentReceipt;
import com.winsdom.result.PageResult;
import com.winsdom.result.Result;
import com.winsdom.service.IPaymentReceiptService;
import com.winsdom.vo.PaymentReceiptVO;
import com.winsdom.vo.resource.ClaimRecordVO;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.winsdom.dto.PaymentDTO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 收款记录表 前端控制器
 * </p>
 *
 * @author
 * @since 2025-05-22
 */
@Slf4j
@RestController
@RequestMapping("/payment-receipt")
@Validated
@Api(tags = "收款记录接口")
public class PaymentReceiptController {
    @Autowired
    private IPaymentReceiptService paymentReceiptService;

    /**
     * 收款流水分页查询
     * @param paymentDTO
     * 收款流水DTO
     * @return
     * 返回PageResult
     */
    @PostMapping("/collectionRecord")
    @ApiOperation(value = "收款流水分页查询")
    public Result collectionRecord(@RequestBody PaymentDTO paymentDTO){
        log.info("收款流水{}",  paymentDTO);
        PageResult pageResult = paymentReceiptService.getCollectionRecord(paymentDTO);
        return Result.success(pageResult);
    }

    /**
     * 通过条件分页查询收款记录
     * @param paymentReceiptDTO
     * 收款记录DTO
     * @return
     * 返回PageResult
     */
    @ApiOperation(value = "条件分页查询收款记录")
    @PostMapping("/listBatch")
    public Result<PageResult> getPaymentReceiptList(@Valid @RequestBody PaymentReceiptDTO paymentReceiptDTO) {
        log.info("查询条件{}", paymentReceiptDTO);
        PageResult page = paymentReceiptService.getPaymentReceiptList(paymentReceiptDTO);
        return Result.success(page);
    }

    /**
     * 生成退款记录
     * @param refundOrderDTO
     * 退款记录DTO
     * @return
     * 返回生成结果
     */
    @ApiOperation(value = "退款操作")
    @PostMapping("/refund")
    public Result refundWithSuccessStatus(@Valid @RequestBody RefundOrderDTO refundOrderDTO){
        log.info("退款信息{}", refundOrderDTO);
        paymentReceiptService.refundWithSuccessStatus(refundOrderDTO);
        return Result.success("退款信息已记录，请等待审核通过");
    }

    /**
     * 删除收款记录
     * @param id
     * 收款记录id
     * @return
     * 返回删除结果
     */
    @ApiOperation(value = "删除收款记录")
    @PostMapping("/deletePaymentReceipt")
    public Result deleteByPaymentIds(@RequestBody Long id){
        log.info("删除的收款记录id为：{}",id);
        paymentReceiptService.deletePaymentReceiptById(id);
        return Result.success("删除收款记录成功");
    }

    /**
     * 导入收款记录
     * @param file
     * 收款记录excel表
     * @return
     * 返回excel表集合
     */
    @ApiOperation(value = "导入收款记录")
    @PostMapping("/importExcel")
    public Result<List<PaymentReceiptVO>> importExcelToData(@RequestParam("file") MultipartFile file){
        log.info("导入excel文件：{}",file);
        paymentReceiptService.importExcelToData(file);
        return Result.success("导入成功");
    }






    @PostMapping("/seleCancelClaimt")
    @ApiOperation("获取当前订单已认领的收款记录（支持条件查询）")
    public PageResult seleCancelClaimt(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页条数") @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestBody CancelClaimtQueryDTO queryDTO) {
        log.info("查询当前订单已认领的收款记录列表,页码：{}，每页条数：{}，查询条件：{}", pageNum, pageSize, queryDTO);
        Page<ClaimRecordVO> page = paymentReceiptService.seleCancelClaimt(pageNum, pageSize, queryDTO);
        return new PageResult(page.getTotal(), page.getRecords(), pageNum, pageSize);
    }
    /**
     * 认领
     * @param claimDTO
     * @return null
     */
    @PostMapping("/claim")
    @ApiOperation(value = "认领")
    public Result<Void> claim(@Valid @RequestBody ClaimDTO claimDTO){
        log.info("认领：{}", claimDTO);
        paymentReceiptService.Claim(claimDTO);
        return Result.success("成功认领");
    }

    /**
     * 取消认领
     * @return
     */
    @PostMapping("/CancelClaim")
    @ApiOperation(value = "取消认领")
    public Result CancelClaim(@Parameter(description = "收款记录ID") @RequestParam("payment_id") List<Integer> paymentId){
        log.info("取消认领：{}", paymentId);
        paymentReceiptService.CancelClaim(paymentId);
        return Result.success("成功撤销认领");
    }
    @PostMapping("/receivepayment")
    @ApiOperation(value = "收款关联记录分页查询")
    public Result<PageResult> receivePayment(@Valid @RequestBody ReceivePaymentDTO receivePaymentDTO){
        log.info("收款：{}", receivePaymentDTO);
        PageResult page = paymentReceiptService.getReceivePaymentByBillsId(receivePaymentDTO);
        return Result.success(page);
    }


}
